package com.hsxc.cdz.controller.consoleController;

import com.hsxc.cdz.base.bean.Bill;
import com.hsxc.cdz.base.bean.ConsoleUser;
import com.hsxc.cdz.base.bean.Pile;
import com.hsxc.cdz.base.util.TimeFormatter;
import com.hsxc.cdz.mgmt.pile.PileManager;
import com.hsxc.cdz.mgmt.station.StationManager;
import com.hsxc.cdz.trans.bill.BillManager;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author 作者: 陈钊
 * @version 创建时间：2016年5月20日 上午10:57:49 类说明 ：請求賬單的controller
 */
@Controller
@RequestMapping("/api")
public class BillController {

    protected static Logger logger = LoggerFactory.getLogger(BillController.class);
    // ==========================================================================================================
    @Autowired
    private BillManager billManager;
    @Autowired
    private PileManager pileRegister;
    @Autowired
    StationManager stationmanager;

    // ===========================================================================================================
    @RequestMapping("/bills")
    @ResponseBody
    public Object loadBillsByYhId(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                  HttpSession httpSession) {
        logger.info("REQUEST BILLS START...");
        List<Bill> bills = new ArrayList<Bill>();
        List<Bill> bills_time = null;
        JSONObject resultJSONObject = new JSONObject();
        resultJSONObject.put("success", true);

        List<Pile> chargePiles = new ArrayList<Pile>();
        try {
            httpServletResponse.setCharacterEncoding("utf-8");
            httpServletResponse.setCharacterEncoding("UTF-8");
            // String token = httpServletRequest.getParameter("token");
            // 获取充电桩编号
            String pileNO = httpServletRequest.getParameter("pileno");
            Set<String> pileNOs = new HashSet<>();
            ConsoleUser consoleuser = new ConsoleUser();
            consoleuser = (ConsoleUser) httpSession.getAttribute("conuser");
            String operatorNO = consoleuser.getOperatorNO();
            String user_name = consoleuser.getUser_name();
            Subject user = SecurityUtils.getSubject();

            if (pileNO == null) {
                if (user.hasRole("vendor")) {
                    logger.info("请求账单");
                    bills = billManager.getAllBills();
                }
                if (user.hasRole("operator")) {
                    logger.info("bills--operatorNo--"+operatorNO);
                    chargePiles = pileRegister.getpileByOperatorNO(operatorNO);
                    for (Iterator<Pile> iterator = chargePiles.iterator(); iterator.hasNext(); ) {
                        Pile chargePile = iterator.next();
                        pileNOs.add(chargePile.getPileNO());
                    }
                    pileNOs.forEach(x->logger.info(x));
                    bills = billManager.getBillsByPileNOs(pileNOs);
                }
                if (user.hasRole("stationAdmin")) {

                    List<String> stationNOs = stationmanager.getStationNOByName(user_name);
                    logger.info("电站编号" + stationNOs);
                    chargePiles = pileRegister.getPileByStationNO(stationNOs);
                    for (Iterator<Pile> iterator = chargePiles.iterator(); iterator.hasNext(); ) {
                        Pile chargePile = iterator.next();
                        pileNOs.add(chargePile.getPileNO());
                    }
                    bills = billManager.getBillsByPileNOs(pileNOs);
                }

            } else {
                if (!user.hasRole("opDeviceManager") && !user.hasRole("pileDeviceManager")) {
                    bills = billManager.getBillsByPileNO(pileNO);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return resultJSONObject;
        }

        if (!bills.isEmpty()) {

            bills_time = new ArrayList<>();
            for (Iterator<Bill> iterator = bills.iterator(); iterator.hasNext(); ) {
                Bill bill = iterator.next();
                bill.setStartTime(TimeFormatter.getTime(bill.getStartTime()));
                bill.setStopTime(TimeFormatter.getTime(bill.getStopTime()));
                bill.setGenerateTime(TimeFormatter.getTime(bill.getGenerateTime()));
                bill.setPayTime(TimeFormatter.getTime(bill.getPayTime()));
                bills_time.add(bill);
            }

            resultJSONObject.put("data", bills_time);
        } else {
            resultJSONObject.put("data", new ArrayList<>());
        }
        logger.info("BILLS RESULT RETURN " + resultJSONObject);
        return resultJSONObject;
    }

}
